 	#include <stdio.h>
    #include <stdlib.h>
    #include <errno.h>
    #include <string.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <sys/un.h>
    #include <sys/stat.h>

	#define SOCK_SERVER 1
    #define SOCK_PATH "/var/mobile/.BSServer"

#include "../src/libAPI.c"

#include "strhex2int.c"

//#include "libAPI.h"


	  int s, s2, t, len; //let everyone see the filedescriptor
	  char send_data[100];
	  
	  int obex_accept = -1;


// Callback functions are defined by application programer
// here as wrappers around calls to application sepecific functions
byte sdp_serve_cb(void) {
	printf("signaling that sdp record was served...\n");
	return 0;
}

int lib_config(void) {
 	// Override NULL callbacks with our own application specific ones
	lib_callbacks->sdp_serve = sdp_serve_cb;
//	lib_callbacks->rfcomm_connect_request = rfcomm_accept_request_cb;
//	lib_callbacks->obex_transfer_request = obex_transfer_request_cb;
//	lib_callbacks->obex_start = obex_start_cb;
//	lib_callbacks->obex_packet_receive = obex_packet_receive_cb;
//	lib_callbacks->obex_done = obex_done_cb;

//	lib_callbacks->device_found = device_found_cb;
//	lib_callbacks->scan_complete = scan_complete_cb;
}

/*-----------------------------------------------------------------------------------*/
//  Main daemon function...
/*-----------------------------------------------------------------------------------*/
int main(int argc, char **argv) {

char return_string[512];
byte hci_scan_mac_addrs[100][6];
byte hci_scan_names[100][200];
byte hci_scan_cods[100][3];	
byte local_device_count;
	
	//make sure this folder is created, if it is already this should fail sliently
	mkdir("/var/mobile/OBEX",777);
	
	
	lib_init();
  	lib_setup();
  	lib_config(); //only for the hci_monitor crap
  	set_scan_enable(3);
	hci_monitor(1);
	

        struct sockaddr_un local, remote;
        char str[100] = "hello";

        if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
            perror("socket");
            exit(1);
        }

        local.sun_family = AF_UNIX;
        strcpy(local.sun_path, SOCK_PATH);
        
        //weeeeeiiiirrddd
        if(unlink("/var/mobile/.BSServe") != 0)
        printf("\n\n\n\nERROR UNLINKING: %s\n\n\n\n",strerror(errno));
        
        len = strlen(local.sun_path) + sizeof(local.sun_family);
        if (bind(s, (struct sockaddr *)&local, len) == -1) {
            perror("bind");
            exit(1);
        }
        	//filesystem actions are fucked
			if(chmod("/var/mobile/.BSServe", 0777) != 0)
			printf("\n\n\n\nERROR CHMODING: %s\n\n\n\n",strerror(errno));
		
        if (listen(s, 5) == -1) {
            perror("listen");
            exit(1);
        }
            int done; 
            int n = 6; //shedule for deletion...


printf("\n\nHi, skram\n\n");

        while(1) {

            
		// infinite accept() loop
            t = sizeof(remote);
            if ((s2 = accept(s, (struct sockaddr *)&remote, &t)) == -1) {
                exit(1);
            }
            
            		if(read(s2,str,100) < 0){
           				printf("error: %s",strerror(errno));
            		}
           			printf("Auth attempt recieved: %s\n",str);
           			
           			
					//this needs to only be asked once...
           			if(strcmp("CMD:AUTH-hfsk&^GHw73",str) == 0){
           				printf("Authorised\n");
	
           			} else {
           			//set an alarm clock really sneakily and quit the daemon
           				exit(1);
           			}
           			
           			memset(str,0,100);
            
            printf("Connected\n");
            
            /************ 
            
            main read loop, read blocks but only on this thread :)
            
            this loop checks for data and calls functions based on what is recieved in CMD:'s
            
            ************/
            
           		while(1){
            	
            		if(read(s2,str,100) == 0){
            			//dont catch the errors (this is a dirty hack)
           				//printf("error: %s",strerror(errno));
           				printf("client closed?\n\n");
           				break;
            		}
           			printf("Recieved Data: %s\n",str);
           			
           			/*
					//this needs to only be asked once...
           			if(strcmp("CMD:AUTH-hfsk&^GHw73",str) == 0){
           				printf("Authorised\n");
	
           			} else {
           			//set an alarm clock really sneakily and quit the daemon
           			}
           			*/
           			
                  	//tentatively, setup as a bunch of strcmp's if the function list gets too long, then change
           			if(strcmp("CMD:hci_scan",str) == 0){
           				printf("Starting HCI scan\n");
	  						
	  					lib_stop(); //stop the lib then do the scan
	  					sleep(5);  					
           				
           				local_device_count = 0;
           				local_device_count = hci_scan((char **) &return_string, 10, 0); // shall block io
           				
           				lib_stop(); sleep(5);
						set_scan_enable(3);
						hci_monitor(1);

printf("return_string: %c",return_string[0]);
    //need to work on return_string and fetch device names...
    // Get mac addr and COD from head of hci_scan_result as bytes
	// 6 bytes for mac addr, one byte (0) for spacer, three bytes for COD
	int j;
	for(j=0; j<6; j++) {
		hci_scan_mac_addrs[local_device_count][j] = hci_scan_result[j];
	}
	for(j=0; j<3; j++) {
		hci_scan_cods[local_device_count][j] = hci_scan_result[j+7];
	}
	printf("   Device #%d : MAC_ADDR=%02X:%02X:%02X:%02X:%02X:%02X  \n", local_device_count+1, 
    		hci_scan_mac_addrs[local_device_count][0], 
    		hci_scan_mac_addrs[local_device_count][1], 
    		hci_scan_mac_addrs[local_device_count][2], 
    		hci_scan_mac_addrs[local_device_count][3], 
    		hci_scan_mac_addrs[local_device_count][4], 
    		hci_scan_mac_addrs[local_device_count][5] );
	printf("                  COD=%02X:%02X:%02X \n",  
    		hci_scan_cods[local_device_count][0], 
    		hci_scan_cods[local_device_count][1], 
    		hci_scan_cods[local_device_count][2]);

	local_device_count++;


           				printf("num deviecs: %d",local_device_count);
						//restart lib
						//set_scan_enable(3);
						//hci_monitor(1);

           				
           			}
           			if(strcmp("CMD:obex:accept",str) == 0){
           			obex_accept = 1;
           			}
           			if(strcmp("CMD:obex:decline",str) == 0){
           			obex_accept = 0;
           			}        			
           			memset(str,0,100);
           			
           			
           		
           		}
        }
        
		unlink(SOCK_PATH);
		

  return 0;
}
/*-----------------------------------------------------------------------------------*/








